﻿using System.Net.Http;
using System.Threading.Tasks;
using System;
using WareHouseManagement.ModelEntity;
using System.Text;
using System.Text.Json;
using WareHouseManagement.Commom;
namespace WareHouseManagement.Data
{
    public class AG2WareHouseServer
    {
        public async Task<string> ClearAG2WareHouse(string remark)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/AG2WareHouse/ClearWareHouse?Remark={remark}");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {

                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }
        }
        public IEnumerable<AG2Warehouse_Entity> GetAG2Warehouse_Entites()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/AG2WareHouse/GetAG2Warehouses");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = client.Send(request);
                        if (response.IsSuccessStatusCode)
                        {

                            var result =  response.Content.ReadAsStream();

                            return JsonSerializer.Deserialize<IEnumerable<AG2Warehouse_Entity>>(result);
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }
        }
        public async Task<string> UpdataAG2(AG2Warehouse_Entity aG2Warehouse_)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/AG2WareHouse/UpdataAG2Warehouse");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                      var jsonPar=JsonSerializer.Serialize<AG2Warehouse_Entity>(aG2Warehouse_);
                        request.Content= new StringContent(jsonPar, Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }
        }
    }
}
